package net.mingsoft.report_data.biz.impl.report_gen;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import net.mingsoft.report_data.biz.ZkDataPeoplesLiveHoodBiz;
import net.mingsoft.report_data.constant.ZkDataConstant;
import net.mingsoft.report_data.dao.ZkDataYqDao;
import net.mingsoft.report_data.entity.GidEntity;
import net.mingsoft.report_data.entity.HotEntity;
import net.mingsoft.report_data.entity.NewIncreaseEntity;
import net.mingsoft.report_data.entity.vo.DateWithCountDto;
import net.mingsoft.report_data.util.MathUtil;
import net.mingsoft.report_data.util.YqDatasourceUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gengfa
 * @date 22/05/19
 */
@Service
@DS("yq")
public class ZkDataPeoplesLiveHoodBizImpl implements ZkDataPeoplesLiveHoodBiz {
    @Resource
    ZkDataYqDao dataYqDao;

    private static final String DESC_ANTI_SMUGGLER = "据统计，近一周岛内反走私宣传以{}报道居多";
    private static final String DESC_ANTI_SMUGGLER_P2 = "其中{}报道量最多，{}其次。{}报道量{}，占比{}%，{}报道量{}，占比{}%。";

    @Override
    public Map<String, Object> getHotEvents(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<HotEntity> hotEntities = dataYqDao.selectInIslandHotEvents(beginDate, endDate);
        map.put("hotEventList", JSON.toJSONString(hotEntities));
        return map;
    }

    @Override
    public Map<String, Object> getPublicOpinionDevelopment(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<NewIncreaseEntity> newIncreaseEntities = dataYqDao.selectInIslandPublicOpinionDevelopment(beginDate, endDate);
        if (newIncreaseEntities.size() > 0) {
            newIncreaseEntities.forEach(e -> {
                e.setDataSource(YqDatasourceUtil.getDatasource(e.getDataSource()));
            });
        }
        map.put("newIncreaseEntities", JSON.toJSONString(newIncreaseEntities));
        return map;
    }

    @Override
    public Map<String, Object> getMediaReportTrending(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<DateWithCountDto> reportTrendingList = dataYqDao.countRecentInIslandPublicOpinions(beginDate, endDate);
        map.put("reportTrendingList", JSON.toJSONString(reportTrendingList));
        map.put("totalPublicOpinionCount",
                reportTrendingList.stream().mapToInt(o-> Integer.parseInt(o.getCount())).sum());
        map.put("dateForMaxValue",
                JSON.toJSONString(
                        reportTrendingList.stream().max(
                                Comparator.comparing(o -> Integer.parseInt(o.getCount()))).orElse(new DateWithCountDto()
                                .setDatasource(YqDatasourceUtil.UNKNOWN_DATASOURCE)
                                .setCount("0")
                                .setDate(beginDate+"~"+endDate))));
        return map;
    }

    @Override
    public Map<String, Object> getPopMediaWithNegativeInfo(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<GidEntity> gidEntities
                = dataYqDao.selectPopMediaWithNegativeInfo(beginDate, endDate);
        for (GidEntity e : gidEntities) {
            if (StringUtils.isBlank(e.getUserName())) {
                if (StringUtils.isNotBlank(e.getWechatName())) {
                    e.setUserName(e.getWechatName());
                } else if (StringUtils.isNotBlank(e.getWeiboName())) {
                    e.setUserName(e.getWeiboName());
                } else if (StringUtils.isNotBlank(e.getAuthor())) {
                    e.setUserName(e.getAuthor());
                } else {
                    e.setUserName(ZkDataConstant.NO_USER_NAME);
                }
            }
        }
        int gidCount = dataYqDao.countPopMediaWithNegativeInfo(beginDate, endDate);
        map.put("gidEntities", JSON.toJSONString(gidEntities.stream().limit(5).collect(Collectors.toList())));
        map.put("gidCount", gidCount);
        return map;

    }

    @Override
    public Map<String, Object> getInIslandAntiSmugglerReportCount(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<DateWithCountDto> reportCountList = dataYqDao.countInIslandAntiSmuggleReport(beginDate, endDate);
        if (reportCountList.size() != 0) {
            final String top3names = reportCountList.stream().limit(3).map(DateWithCountDto::getDatasource).collect(Collectors.joining("、"));
            String basicDesc = MessageFormatter.format(DESC_ANTI_SMUGGLER, top3names).getMessage();
            reportCountList.forEach(e -> {
                e.setDatasource(YqDatasourceUtil.getDatasource(e.getDatasource()));
            });
            if (reportCountList.size() >= 2) {
                final long total = reportCountList.stream().mapToLong(o -> Integer.parseInt(o.getCount())).sum();
                final DateWithCountDto dto0 = reportCountList.get(0);
                final DateWithCountDto dto1 = reportCountList.get(1);
                map.put("antiSmuggleDesc", basicDesc + ","
                        + MessageFormatter.arrayFormat(DESC_ANTI_SMUGGLER_P2,
                        new String[]{
                                dto0.getDatasource(), dto1.getDatasource(),
                                dto0.getDatasource(), dto0.getCount(), MathUtil.getRate(Long.parseLong(dto0.getCount()), total),
                                dto1.getDatasource(), dto1.getCount(), MathUtil.getRate(Long.parseLong(dto1.getCount()), total)}).getMessage());
            } else {
                map.put("antiSmuggleDesc", basicDesc);
            }
        } else {
            map.put("antiSmuggleDesc", ZkDataConstant.NO_DATA_DESC);
        }

        map.put("reportCountList", JSON.toJSONString(reportCountList));
        return map;
    }
    @Override
    public Map<String, Object> getSpecificEmployeeDto(Map<String, Object> map) {
        return map;
    }
}
